1891D - Suspicious logarithms - CodeForces Solution


binary search math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>

using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const int mod = 1e9 + 7;
const int MAX = 5e5 + 7;


ll g(ll x) {
    ll pw = __lg(x);
    if (pw <= 1) return 0;
    ll cnt = 1;
    while (x / pw >= __lg(x)) {
        cnt++;
        pw *= __lg(x);
    }
    return cnt;
}

vector<pair<ll, ll>> fs;

ll f(ll x) {
    ll res = 0;
    for (int i = 0; i < (int)(fs.size()) - 1; i++) {
        ll l = fs[i].first;
        ll r = fs[i + 1].first;
        if (l == r) continue;
        r--;
        r = min(r, x);
        if (l > r) continue;
        r = r - l + 1;
        r %= mod;
        res += (r * fs[i].second) % mod;
        res %= mod;
    }
    return res;
}

void solve() {
    for (ll lg = 2; lg <= 60; lg++) {
        ll pw = 1;
        for (ll cnt = 1; cnt <= 80; cnt++) {
            pw *= lg;
            if (__lg(pw) == lg) {
                if (pw > 4) {
                    fs.push_back({pw - 1, g(pw - 1)});
                }
                fs.push_back({pw + 1, g(pw + 1)});
                fs.push_back({pw, g(pw)});
            }
        }
        fs.push_back({(1LL << lg), g((1LL << lg))});
        // cout << '\n';
    }
    sort(fs.begin(), fs.end());
    int t;
    cin >> t;
    while (t--) {
        ll l, r;
        cin >> l >> r;
        ll r1 = f(r);
        ll r2 = f(l - 1);
        r1 -= r2;
        while (r1 < 0) r1 += mod;
        cout << r1 << '\n';
    }
}



int main() {
#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II